<!doctype html>
<html xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <title>文件列表</title>
    <meta charset="UTF-8">

    <script src="../../../js/jquery-2.0.3.min.js"></script>
    <link rel="stylesheet" href="../../../css/bootstrap.min.css">
    <script src="../../../js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../../../css/bootstrapValidator.css">
    <script src="../../../js/bootstrapValidator.js"></script>
    <script src="../../../js/jquery.cookie.js"></script>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="../../../js/hostConfig.js"></script>
</head>
<body>
<div class="container kv-main">

    <div class="jumbotron">
        <h1>添加用户</h1>
        <p></p>
    </div>

    <form name="form" action="/p/add/submit" method="post" enctype="multipart/form-data" class="form-horizontal"
          id="app">


        <div class="form-group">
            <label class="col-lg-1 control-label">用户名称</label>
            <div class="col-lg-6">
                <input type="text" id="name" name="name" class="form-control" placeholder="用户名称" v-model="name"/>
            </div>
        </div>


        <div class="form-group">
            <label class="col-lg-1 control-label">权限手机</label>
            <div class="col-lg-6">
                <input type="text" id="mobilephone" name="mobilephone" class="form-control" placeholder="用户手机"
                       v-model="mobilephone"/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-1 control-label">用户密码</label>
            <div class="col-lg-6">
                <input type="password" id="password" name="password" class="form-control" placeholder="密码"
                       v-model="password"/>
            </div>
        </div>


        <div class="form-group">
            <label class="col-lg-1 control-label">确认密码</label>
            <div class="col-lg-6">
                <input type="password" name="passwordAffirm" id="p2" placeholder="确认密码"
                       class="form-control">
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-1 control-label"></label>
            <div class="col-lg-4 col-lg-offset-1">
                <a class="btn btn-info btn-block" href="javascript:;" v-on:click="submitUserInfo">提交</a>
            </div>
        </div>
    </form>
</div>

</body>
</html>

<script>
    var app = new Vue({
        el: '#app',
        data: {
            name: "",
            mobilephone: "",
            password: "",
        },
        methods: {
            submitUserInfo: function () {


                $.ajax({
                    type: "post",
                    url: host_tomcatR + '/user/add',
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    data: {
                        name: this.name,
                        mobilephone: this.mobilephone,
                        password: this.password
                    },
                    success: function (data) {
                        if (data["code"] != "0000") {
                            alert(response.data.description);
                            console.log(response.data);
                        } else {
                            window.location.href = "/pages/manage/user/list.html";
                        }
                    },
                    error: function (xhr, type) {
                        console.log(xhr);
                    }
                });

//                axios({
//                    method: 'post',
//                    url: host_tomcatR + '/user/add',
//                    data: {
//                        name: this.name,
//                        mobilephone: this.mobilephone,
//                        password: this.password
//                    },
//                    withCredentials: true
//                }).then(function (response) {
//                    if (response.data.code != "0000") {
//                        alert(response.data.description);
//                        console.log(response.data);
//                    } else {
//                        window.location.href = "/pages/manage/user/list.html";
//                    }
//                });
            }
        }
    });
</script>

<script>
    $(function () {
        $('#app').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                mobilephone: {
                    validators: {
                        notEmpty: {
                            message: '手机不能为空'
                        },
                        stringLength: {
                            /*长度提示*/
                            min: 11,
                            max: 11,
                            message: '手机长度必须是11位'
                        },
                        remote: {//ajax验证。server result:{"valid",true or false} 向服务发送当前input name值，获得一个json数据。例表示正确：{"valid",true}
                            url: host_tomcatR + '/register/UserExists',//验证地址
                            message: '用户已存在',//提示消息
                            delay: 1000,//每输入一个字符，就发ajax请求，服务器压力还是太大，设置2秒发送一次ajax（默认输入一个字符，提交一次，服务器压力太大）
                            type: 'POST'//请求方式
                        }
                    },
                    threshold: 11//有11字符以上才发送ajax请求，（input中输入一个字符，插件会向服务器发送一次，设置限制，11字符以上才开始）
                },
                password: {
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        },
                        stringLength: {
                            /*长度提示*/
                            min: 6,
                            max: 20,
                            message: '手机长度必须大于6位'
                        }/*最后一个没有逗号*/
                    }
                },
                passwordAffirm: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        identical: {//相同
                            field: 'password', //需要进行比较的input name值
                            message: '两次密码不一致'
                        },
                    }
                }
            }
        });
    });

</script>
